package milu.servlets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;

import milu.*;
import milu.models.*;
import com.google.appengine.api.datastore.*;

public class RecipePage extends HttpServlet {
    public void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        String[] segments = request.getRequestURI().split("/");
        String recipeId = null;
        if(segments.length >= 3)
            recipeId = segments[2];

        // find the recipe
        PersistenceManager pm = Datastore.getPersistenceManager();
        Key k = KeyFactory.createKey(Recipe.class.getSimpleName(), Long.parseLong(recipeId));
        Recipe recipe = pm.getObjectById(Recipe.class, k);
        request.setAttribute("recipe", recipe);

        // get author of this recipe
        MyUser recipeAuthor = MyUser.getUserInfo(recipe.getUserKey());
        request.setAttribute("recipeAuthor", recipeAuthor);

        // get comments for this recipe
        List<RecipeComment> comments = RecipeComment.getCommentsForRecipe(recipe.getKey());
        request.setAttribute("comments", comments);
        
        request.getRequestDispatcher("/recipe-page.jsp").forward(request, response);
    }
}
